package com.tongguan.paypos.activity;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tongguan.paypos.R;
import com.tongguan.paypos.activity.menu.PrintManageActivity;
import com.tongguan.paypos.activity.menu.SettingManageActivity;
import com.tongguan.paypos.activity.menu.TransBillsActivity;
import com.tongguan.paypos.activity.menu.TransSettleActivity;
import com.tongguan.paypos.activity.trans.MemberRechargeActivity;
import com.tongguan.paypos.activity.trans.MoneyInputActivity;
import com.tongguan.paypos.activity.trans.RevokeManageActivity;
import com.tongguan.paypos.base.BaseActivity;
import com.tongguan.paypos.common.AppConstants;
import com.tongguan.paypos.utils.AppUtils;
import com.tongguan.paypos.utils.CipherUtils;
import com.tongguan.paypos.utils.ToastUtils;
import com.tongguan.paypos.view.CustomInputDialog;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity {

    @BindView(R.id.toolbar_setting)
    ImageButton mButtonSetting;
    @BindView(R.id.tv_pay_pos)
    TextView mTvPayPos;
    @BindView(R.id.tv_pay_qr)
    TextView mTvPayQr;
    @BindView(R.id.tv_pay_revocation)
    TextView mTvPayRevocation;
    @BindView(R.id.module_pay)
    LinearLayout mModulePay;
    @BindView(R.id.module_member)
    LinearLayout mModuleMember;
    @BindView(R.id.module_print)
    LinearLayout mModulePrint;
    @BindView(R.id.module_union_pay_manage)
    LinearLayout mModuleUnionPayManage;
    @BindView(R.id.module_bills)
    LinearLayout mModuleBills;
    @BindView(R.id.module_load_settlement)
    LinearLayout mModuleLoadSettlement;
    @BindView(R.id.toolbar_title)
    TextView mToolbarTitle;

    private int count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initTile(R.string.app_name, false);
        mButtonSetting.setVisibility(View.VISIBLE);
        initEvent();
    }

    private void initEvent() {
        //跳转到setting 界面
        mButtonSetting.setOnClickListener(v -> startActivity(new Intent(Settings.ACTION_SETTINGS)));
        //-----------------------------------------
        //银联刷卡快捷方式
        mTvPayPos.setOnClickListener(v -> shortcut("2", false));

        //付款码
        mTvPayQr.setOnClickListener(v -> shortcut("1", true));

        //撤销
        mTvPayRevocation.setOnClickListener(v -> jumpAct(RevokeManageActivity.class, true));

        //-----------------------------------------
        //支付
        mModulePay.setOnClickListener(v -> shortcut("0", true));

        //会员充值
        mModuleMember.setOnClickListener(v -> jumpAct(MemberRechargeActivity.class, true));

        //打印
        mModulePrint.setOnClickListener(v -> jumpAct(PrintManageActivity.class, true));

        //收银管理
        mModuleUnionPayManage.setOnClickListener(v -> AppUtils.runComponent(context, AppConstants.UNION_PAY_APP_PACKAGE, AppConstants.UNION_PAY_APP_MAIN, null));

        //账单
        mModuleBills.setOnClickListener(v -> jumpAct(TransBillsActivity.class, true));

        //结算
        mModuleLoadSettlement.setOnClickListener(v -> jumpAct(TransSettleActivity.class, true));

        mToolbarTitle.setOnClickListener(v -> setting());

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_info:
                jumpAct(DeviceInfoActivity.class);
                return true;
//               case R.id.action_download:
//               jumpAct(UpdateActivity.class);
//               return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void shortcut(String state, boolean isLogin) {
        //走银联快捷支付 不需要登录。
        Bundle bundle = new Bundle();
        bundle.putString(MoneyInputActivity.DEFAULT_INPUT, state);
        jumpAct(MoneyInputActivity.class, bundle, isLogin, false);
    }

    private void setting() {
        if (count > 7) {
            new CustomInputDialog.Builder(context).setTitle("请输入管理密码")
                    .setPositiveButton(R.string.common_sure, pwd -> {
                        if (CipherUtils.KEY_8.equals(CipherUtils.md5(pwd))) {
                            jumpAct(SettingManageActivity.class);
                            count = 0;
                        } else {
                            ToastUtils.showCustom(context, "密码错误");
                        }
                    }).setNegativeButton(R.string.common_cancel, (dialog, which) -> count = 0).show();
        } else {
            count++;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) { //屏蔽返回键
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onPause() {
        super.onPause();
        count = 0;
    }
}
